package com.example.cursorsearch;

import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserFactory;

import com.example.cursorsearch.R;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.*;
import android.view.View.OnClickListener;
import android.widget.*;

 public class SubActivity extends Activity {
	 
	 Intent intent;
	 
	 @Override
	 public void onCreate(Bundle savedInstanceState) {
		 super.onCreate(savedInstanceState);
		 setContentView(R.layout.bus);
		 
		 Bundle bundle = getIntent().getExtras();
		 String busnum = bundle.getString("num");
		 
		
		 ArrayList<String> i = new ArrayList<String>();
		 
		 try {
			 String serviceUrl = "http://ws.bus.go.kr/api/rest/busRouteInfo/getBusRouteList";
			 String serviceKey = "OKJLjAtvWjTEGvHi7Iyzmx7LhXqLrGQaGdnVV75FkZ4ZiRNrOAw61cW5sayWE9Q3xFt989joOdoGI/30Ousbtw=="; // OpenAPI Key
             serviceKey = URLEncoder.encode(serviceKey);
           
             String strSrch = busnum;
             String strURL = serviceUrl + "?ServiceKey=" + serviceKey + "&strSrch=" + strSrch;
             
             URL url = new URL(strURL);
             
             XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
             XmlPullParser parser = factory.newPullParser();
             parser.setInput(url.openStream(), "utf-8");
             
             int eventType = parser.getEventType();
             
             boolean bSet = false;
             boolean aSet = false;
             
             while (eventType != XmlPullParser.END_DOCUMENT) {
            	 switch (eventType) {
            	    case XmlPullParser.START_DOCUMENT:
            	    	break;
            	    	
            	    case XmlPullParser.START_TAG:
            	    	String tag = parser.getName();
            	    	if(tag.equals("busRouteId")) {
            	    		// (tag.equals("busRouteNm") || tag.equals("stStationNm") || tag.equals("edStationNm")) {
            	    		bSet = true;
            	    	}
            	    	break;
            	    	
            	    case XmlPullParser.END_TAG:
            	    	break;
            	    	
            	    case XmlPullParser.TEXT:
            	    	if (bSet) {
            	    		String content = parser.getText();
            	    		String Url = "http://ws.bus.go.kr/api/rest/busRouteInfo/getStaionByRoute?ServiceKey=OKJLjAtvWjTEGvHi7Iyzmx7LhXqLrGQaGdnVV75FkZ4ZiRNrOAw61cW5sayWE9Q3xFt989joOdoGI/30Ousbtw==&busRouteId=" + content;
            	    		URL urL = new URL(Url);
            	    		XmlPullParserFactory factory1 = XmlPullParserFactory.newInstance();
            	            XmlPullParser parser1 = factory.newPullParser();
            	            parser1.setInput(urL.openStream(), "utf-8");
            	             
            	             int eventType1 = parser1.getEventType();
            	             while (eventType1 != XmlPullParser.END_DOCUMENT) {
            	            	 switch (eventType1) {
            	            	    case XmlPullParser.START_DOCUMENT:
            	            	    	break;
            	            	    	
            	            	    case XmlPullParser.START_TAG:
            	            	    	String tag1 = parser1.getName();
            	            	    	if(tag1.equals("stationNm") || tag1.equals("station")) {
            	            	    		
            	            	    		aSet = true;
            	            	    	}
            	            	    	break;
            	            	    	
            	            	    case XmlPullParser.END_TAG:
            	            	    	break;
            	            	    	
            	            	    case XmlPullParser.TEXT:
            	            	    	if (aSet) {
            	            	    		String b = parser1.getText();
            	            	    		i.add(b);
            	     
            	            	    	ArrayAdapter<String> adapter2 = 
            	            	    	new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, i);
            	           
            	            	   	   ListView list2 = (ListView)findViewById(R.id.listView2);
            	            	   	   list2.setAdapter(adapter2);
            	            	    		aSet = false;
            	            	    	}
            	            	    	break;
            	            	    case XmlPullParser.END_DOCUMENT:
            	            	    	break;
            	            	    	//switch (eventType1)
            	            	 }
            	            	 eventType1 = parser1.next();
            	            	 
            	            	//while (eventType1 != XmlPullParser.END_DOCUMENT)  
            	             }
            	            	    	
            	    		//String a = parser.getText();
        
            	    		
            	    		//editText1.append(a + "\n");
            	    		bSet = false;
            	    		//if(bSet)
            	    	}
            	    	break;
            	    	
            	    case XmlPullParser.END_DOCUMENT:
            	    	break;
            	    	//switch
            	 }
            	 
            	 eventType = parser.next();
            	 //while
             }
		 //try
		 } catch (Exception e) {
			 Toast.makeText(getBaseContext(), e.getMessage(), 0).show();
		 }
	 }
 }

  